package com.teamall.web.fpgz.controller;

import java.util.List;

import com.teamall.framework.util.JsonUtil;
import com.teamall.web.sys.db.po.TownAidOrgPO;
import com.teamall.web.sys.service.ITownAidOrgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.teamall.core.common.JModelAndView;
import com.teamall.framework.exception.BaseAppException;
import com.teamall.web.base.db.po.TownInfoPO;
import com.teamall.web.base.db.po.VillageInfoPO;
import com.teamall.web.base.service.ITownInfoService;
import com.teamall.web.base.service.IVillageInfoService;
import com.teamall.web.main.db.po.CodeStatTownPO;
import com.teamall.web.main.db.po.CodeStatVillagePO;
import com.teamall.web.main.service.ICodeStatTownService;
import com.teamall.web.main.service.ICodeStatVillageService;

/**
 * ${DESCRIPTION}
 *
 * @author
 * @create 2017-07-29 10:08
 **/
@Controller
@RequestMapping("/fpgz")
public class FpgzController {
    @Autowired
    private ITownInfoService townInfoService;
    @Autowired
    private IVillageInfoService villageInfoService;
    @Autowired
    private ICodeStatVillageService codeStatVillageService;
    
    @Autowired
    private ICodeStatTownService codeStatTownService;
    @Autowired
    private ITownAidOrgService townAidOrgService;
    @RequestMapping("/")
    public ModelAndView index() throws BaseAppException{
        ModelAndView modelAndView = new JModelAndView("/fpgz/fpgz_main");
        List<TownInfoPO> townInfoPOList = townInfoService.selectByArg(new TownInfoPO());
        List<VillageInfoPO> villageInfoPOList = villageInfoService.selectByArg(new VillageInfoPO());
        for (TownInfoPO townInfoPO: townInfoPOList){
            for (VillageInfoPO villageInfoPO: villageInfoPOList){
                if (townInfoPO.getTownId() == villageInfoPO.getTownId()){
                    townInfoPO.getVillageInfoPOList().add(villageInfoPO);
                }
            }
        }
        modelAndView.addObject("townInfoPOList",townInfoPOList);
        return modelAndView;
    }
    @RequestMapping("/content")
    public ModelAndView content(int townId) throws BaseAppException{
        TownInfoPO townInfoPO = townInfoService.selectByPrimaryKey(townId);
        ModelAndView modelAndView = new JModelAndView("fpgz/content");
        modelAndView.addObject("townInfoPO",townInfoPO);
        List<CodeStatVillagePO> codeStatVillagePOList = codeStatVillageService.selectByArg(townId);
        String pkListStr =  JsonUtil.toJson(codeStatVillagePOList);
        modelAndView.addObject("pkListStr", pkListStr);
        modelAndView.addObject("codeStatVillagePOList",codeStatVillagePOList);
        CodeStatTownPO statTownPO = codeStatTownService.selectByPrimaryKey(townId);
        modelAndView.addObject("statTownPO",statTownPO);
        TownAidOrgPO townAidOrgPO = new TownAidOrgPO();
        townAidOrgPO.setTownId(townId);
        List<TownAidOrgPO> townAidOrgPOList = townAidOrgService.selectByArg(townAidOrgPO);
        if(townAidOrgPOList.size()>0){
            TownAidOrgPO result = townAidOrgPOList.get(0);
            modelAndView.addObject("townAidOrg",result);
        }
        return modelAndView;
    }
}
